package com.sticktogether.navigation;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.sticktogether.R;

/**
 * Adapter for the ExpandableListView
 * @author David Schattel
 *
 */
public class ExpandableListAdapter extends BaseExpandableListAdapter{
@Override
    public boolean areAllItemsEnabled()
    {
        return true;
    }


    private Context context;
    private ArrayList<Group> groups;
    private ArrayList<ArrayList<Child>> children;
    public ExpandableListAdapter(Context context, ArrayList<Group> groups, ArrayList<ArrayList<Child>> children) {
        this.context = context;
        this.groups = groups;
        this.children = children;
    }

    public Object getChild(int groupPosition, int childPosition) {
    	if(children.get(groupPosition).size() > childPosition && children.get(groupPosition) != null)
    		return children.get(groupPosition).get(childPosition);
    	else
    		return null;
    }


    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }
    
    // Return a child view. You can load your custom layout here.
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        Child child = (Child) getChild(groupPosition, childPosition);
        if(child != null){
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.list_child_layout, null);
	            
	        }
	        TextView tv = (TextView) convertView.findViewById(R.id.t_Child);
	        tv.setText("" + child.getText());
	
	        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
        }
        return convertView;
    }


    public int getChildrenCount(int groupPosition) {
    	if(children.size() > groupPosition && children.get(groupPosition) != null){
    		return children.get(groupPosition).size();
    	}
    	else{
    		return 0;
    	}
    }


    public Object getGroup(int groupPosition) {
    	if(groups.size() > groupPosition)
    		return groups.get(groupPosition);
    	else
    		return null;
    }


    public int getGroupCount() {
        return groups.size();
    }


    public long getGroupId(int groupPosition) {
        return groupPosition;
    }


    // Return a group view. You can load your custom layout here.
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if(group != null){
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.list_group_layout, null);
	        }
	        TextView tv = (TextView) convertView.findViewById(R.id.t_Group);
	        tv.setText(group.getText());
	        
	        tv.setCompoundDrawablesWithIntrinsicBounds(group.getIcon(), null, null, null);
        	        }
        return convertView;
    }
    
    
    public boolean hasStableIds() {
        return true;
    }


    public boolean isChildSelectable(int arg0, int arg1) {
        return true;
    }
}